package logic

import (
	"context"
	"srv/model/mysql"

	"srv/internal/svc"
	"srv/shenzhou"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListUserLogic {
	return &ListUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListUserLogic) ListUser(in *shenzhou.ListUserRequest) (*shenzhou.ListUserResponse, error) {
	u := mysql.User{}
	user, err := u.GetUser(int(in.State))
	if err != nil {
		logx.Error("获取用户列表失败", err)
		return nil, err
	}
	if u.State != 0 {
		logx.Error("获取用户已冻结", err)
		return nil, err
	}
	var userList []*shenzhou.UserItem
	for _, v := range user {
		userList = append(userList, &shenzhou.UserItem{
			UserId:   int64(v.ID),
			Username: v.Username,
			Password: v.Password,
			Image:    v.Image,
		})
	}

	return &shenzhou.ListUserResponse{
		UserList: userList,
	}, nil
}
